package com.zdy.im.user.service.impl;


import com.sixeco.framework.common.data.BasePage;
import com.sixeco.framework.common.enums.ResultEnum;
import com.sixeco.framework.core.db.ext.Query;
import com.sixeco.framework.http.data.vo.ResponseResultList;
import com.sixeco.framework.util.PageUtils;
import com.sixeco.framework.util.excel.ExcelUtils;
import com.zdy.im.user.service.ImUserFriendShipService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;


import com.zdy.im.user.entity.ImUserFriendShip;
import com.zdy.im.user.dao.ImUserFriendShipDao;
import com.zdy.im.user.dto.request.ImUserFriendShipRequestDto;
import com.zdy.im.user.dto.response.ImUserFriendShipResponseDto;
import org.springframework.util.CollectionUtils;

import java.io.InputStream;


/**
 * @ClassName: ImUserFriendShipDao.java
 * @Description: 用户好友列关系表的数据交互处理类
 * @author xxx
 * @date   2021-05-08 17:46:09
 * @version V1.0.0
 * <p>
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 * </p>
 */
@Service
public class ImUserFriendShipServiceImpl  implements ImUserFriendShipService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ImUserFriendShipDao  imUserFriendShipDao;



    /**
     * 保存用户好友列关系表同时更新数据库和缓存的实现方法
     * @param imUserFriendShipRequestDto ImUserFriendShipResponseDto
     * @return
     */
    public ImUserFriendShipResponseDto saveImUserFriendShip(ImUserFriendShipRequestDto imUserFriendShipRequestDto){
        if(imUserFriendShipRequestDto== null){
            return null;
        }
        ImUserFriendShip imUserFriendShip = ImUserFriendShip.build().clone(imUserFriendShipRequestDto);
        boolean result = imUserFriendShipDao.save(imUserFriendShip);
        if(!result){
            return null;
        }
        ImUserFriendShipResponseDto imUserFriendShipResponseDto = ImUserFriendShipResponseDto.build().clone(imUserFriendShip);
        return imUserFriendShipResponseDto;


    }

    public boolean saveBatchImUserFriendShip(Collection<ImUserFriendShipRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() !=  batchSize){
            return false;
        }
        List<ImUserFriendShip> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImUserFriendShip.build().clone(e)));
        boolean result = imUserFriendShipDao.saveBatch(list,batchSize);
        return result;
    }


    public boolean saveOrUpdateBatchImUserFriendShip(Collection<ImUserFriendShipRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() != batchSize ){
            return false;
        }
        List<ImUserFriendShip> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImUserFriendShip.build().clone(e)));
        boolean result = imUserFriendShipDao.saveOrUpdateBatch(list,batchSize);
        return result;
    }


    public boolean updateBatchById(Collection<ImUserFriendShipRequestDto> entityList, int batchSize) {
        if(null == entityList || entityList.size() != batchSize ){
            return false;
        }
        List<ImUserFriendShip> list = new ArrayList<>();
        entityList.forEach(e -> list.add(ImUserFriendShip.build().clone(e)));
        boolean result =  imUserFriendShipDao.updateBatchById(list,batchSize);
        return result;
    }

    /**
     * 更新用户好友列关系表同时更新数据库和缓存的实现方法
     * @param imUserFriendShipRequestDto  ImUserFriendShipResponseDto
     * @return
     */
    public boolean updateImUserFriendShip(ImUserFriendShipRequestDto imUserFriendShipRequestDto){

          if(null ==  imUserFriendShipRequestDto){
              return false;
          }

        ImUserFriendShip imUserFriendShip = ImUserFriendShip.build().clone(imUserFriendShipRequestDto);

        return imUserFriendShipDao.updateById(imUserFriendShip);


    }







    /**
     * 通过主键ID删除对象信息实现缓存和数据库,同时删除的方法
     * @param  idList
     * @return
     */
    public int delImUserFriendShipByIds(List<Long> idList){
        boolean result = false;
        if(null == idList){
            return ResultEnum.FAIL.id();
        }
        if( idList.size() == 1) {
            result = imUserFriendShipDao.removeById(idList.get(0));
        }else {
            result =  imUserFriendShipDao.removeByIds(idList);
        }
        if(result){
            return ResultEnum.SUCCESS.id();
        }
        return ResultEnum.FAIL.id();

    }


    /**
     * 通过主键ID更新对象用户好友列关系表实现缓存和数据库更新的方法
     * @param  imUserFriendShipId
     * @return
     */
    public ImUserFriendShipResponseDto getImUserFriendShipById(long imUserFriendShipId){
        if(imUserFriendShipId < 0 ){
            return null;
        }
        ImUserFriendShip imUserFriendShip =  imUserFriendShipDao.getById(imUserFriendShipId);
        if(imUserFriendShip == null){
            return null;
        }
        ImUserFriendShipResponseDto imUserFriendShipResponseDto = ImUserFriendShipResponseDto.build().clone(imUserFriendShip);

        return imUserFriendShipResponseDto ;

    }



    /**
    * 查询条件
    */
    public QueryWrapper<ImUserFriendShip> checkQueryCondition(BasePage basePage) {
        QueryWrapper<ImUserFriendShip> queryWrapper = new QueryWrapper<>();
        ImUserFriendShipRequestDto imUserFriendShipRequestDto = (ImUserFriendShipRequestDto) basePage.getParamObject();

                                                                                                                                                                                                                        return queryWrapper;
    }


    /**
     * 通过分页获取ImUserFriendShip信息实现查找缓存和数据库的方法
     * @param basePage BasePage
     * @return
     * @author xxx
     * @date 2021-05-08 17:46:09
     */
    public List<ImUserFriendShipResponseDto> getImUserFriendShipByPage(
            ResponseResultList<ImUserFriendShipResponseDto> responseResultList, BasePage basePage){


        List<ImUserFriendShipResponseDto> resDtoList = new ArrayList<>();
        if(basePage == null){
            return resDtoList;
        }
        //分页对象        PageHelper
        IPage<ImUserFriendShip> iPage = new Page<>(basePage.getPageNo(), basePage.getRealPageSize());
        QueryWrapper<ImUserFriendShip> queryWrapper = checkQueryCondition(basePage);

        IPage<ImUserFriendShip> page = imUserFriendShipDao.page(iPage, queryWrapper);
        if(null == page){
            return resDtoList;
        }

        List<ImUserFriendShip>  list = page.getRecords();
        if(null == list || list.isEmpty()){
            return resDtoList;
        }
        list.forEach(imUserFriendShip -> {
                ImUserFriendShipResponseDto imUserFriendShipResponseDto = ImUserFriendShipResponseDto.build().clone(imUserFriendShip);

            resDtoList.add(imUserFriendShipResponseDto);
        });
        responseResultList.setTotal((int) page.getTotal());
        return resDtoList;


    }

    /**
     * 通过分页获取ImUserFriendShip信息实现查找缓存和数据库的方法
     * @param basePage BasePage
     * @return
     * @author xxx
     * @date 2021-05-08 17:46:09
     */
    public ResponseResultList<ImUserFriendShipResponseDto> getImUserFriendShipByNextPage(BasePage basePage){
        ResponseResultList<ImUserFriendShipResponseDto> responseResultList = ResponseResultList.build();
        List<ImUserFriendShipResponseDto>  list = this.getImUserFriendShipByPage(responseResultList,basePage);
        if(null == list ){
            list = new ArrayList<>();
        }
        boolean isNext =  basePage.isNextPage(list);
        responseResultList.toIsNextPage(isNext).toList(list);
        return responseResultList;

    }

    public PageUtils queryPage(Map<String, Object> params) {
        IPage iPage =  new Query<ImUserFriendShip>().getPage(params);
        QueryWrapper<ImUserFriendShip> queryWrapper = new QueryWrapper<>();
        IPage<ImUserFriendShip> page = imUserFriendShipDao.page(iPage,queryWrapper);
        return new PageUtils(page);
    }


    /**
     * 保存用户好友列关系表同时更新数据库和缓存的实现方法
     * @return
     */
    public ImUserFriendShip  setImUserFriendShip(){
        ImUserFriendShip imUserFriendShip = new ImUserFriendShip();
        /**
 			//imUserFriendShip.setCreateDate (Date createDate);
 			//imUserFriendShip.setModifyDate (Date modifyDate);
 			//imUserFriendShip.setStatus (int status);
 			//imUserFriendShip.setUserId (long userId);
 			//imUserFriendShip.setFriendId (long friendId);
 			//imUserFriendShip.setFriendRemarks (String friendRemarks);
 			//imUserFriendShip.setUserGroupId (long userGroupId);
 			//imUserFriendShip.setUserGroupName (String userGroupName);
 			//imUserFriendShip.setRemarks (String remarks);
 			//imUserFriendShip.setSort (int sort);
		**/

        return imUserFriendShip;
    }

    public boolean saveData(InputStream initialStream) {
        return ExcelUtils.readExcel(initialStream,imUserFriendShipDao, ImUserFriendShip.class,0);
    }

    /**
     * 查询某个用户好友关系
     * @param userId
     * @param friendId
     * @return
     */
    public ImUserFriendShipResponseDto selectFriend(long userId,long friendId){
        ImUserFriendShip imUserFriend = imUserFriendShipDao.selectFriend( userId, friendId);
        if(imUserFriend == null){
            return null;
        }
        /*List<ImUserFriendShipResponseDto> lists = new ArrayList<>();
        list.forEach(e->{
            lists.add();
        });*/
        return ImUserFriendShipResponseDto.build().clone(imUserFriend);
    }

    /**
     * 查询某个用户，所有好友列表
     * @param userId
     * @return
     */
    public List<ImUserFriendShipResponseDto> selectListByUserId(long userId){
        List<ImUserFriendShip> list = imUserFriendShipDao.selectListByUserId( userId);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        List<ImUserFriendShipResponseDto> lists = new ArrayList<>();
        list.forEach(e->{
            lists.add(ImUserFriendShipResponseDto.build().clone(e));
        });
        return lists;
    }

    /**
     * 查询某个用户，所有好友列表
     * @param userId
     * @param name
     * @return
     */
    public List<ImUserFriendShipResponseDto> selectListByUserId(long userId,String name){
        List<ImUserFriendShip> list = imUserFriendShipDao.selectListByUserId( userId,name);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        List<ImUserFriendShipResponseDto> lists = new ArrayList<>();
        list.forEach(e->{
            lists.add(ImUserFriendShipResponseDto.build().clone(e));
        });
        return lists;
    }

    /**
     * 查询某用户，某组下好友的最大序号
     * @param userId
     * @param userGroupId
     * @return
     */
    public ImUserFriendShipResponseDto selectOneByUserIdAndGroupId(long userId,long userGroupId){
        ImUserFriendShip imUserFriend = imUserFriendShipDao.selectOneByUserIdAndGroupId( userId,userGroupId);
        if(imUserFriend == null){
            return null;
        }
        return ImUserFriendShipResponseDto.build().clone(imUserFriend);
    }

    /**
     * 查询某个组下，所有好友列表
     * @param groupId
     * @return
     */
    public List<ImUserFriendShipResponseDto> selectListByUserGroupId(long groupId){
        List<ImUserFriendShip> list = imUserFriendShipDao.selectListByUserGroupId( groupId);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        List<ImUserFriendShipResponseDto> lists = new ArrayList<>();
        list.forEach(e->{
            lists.add(ImUserFriendShipResponseDto.build().clone(e));
        });
        return lists;
    }

}
